<?php

class Producto_model extends CI_Model {

    public function list_productos($params, $prod) {
        $sql = "call sp_ListarProductos(?,?,?,?)";
        $query = $this->db->query($sql, $params);
        if ($prod != 0) {
            return $query;
        }
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    public function filtrarProductos($params) {
        $sql = "call sp_filtrarProductos(?,?)";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    public function list_StockProd($params) {
        $sql = "call sp_ListarStockProd(?,?,?)";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    public function list_StockProdxColor($params) {
        $sql = "call sp_listarStockxColor(?,?,?)";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    public function list_preciosProdxSuc($params) {
        $sql = "call sp_ListPreciosProducto(?,?)";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    public function list_colorxprod($cod) {
        $sql = "call sp_colorxprod(?)";
        $query = $this->db->query($sql, $cod);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    public function IU_Producto($des, $modelo, $marca, $familia, $suc, $prod, $precios, $colors) {
        try {
            $this->db->trans_start();
            $sql = "call sp_IULogProducto(?,?,?,?,?,@id)";
            $params = array($des, $modelo, $marca, $familia, $suc);
            $this->db->query("select $prod into @id");
            $this->db->query($sql, $params);
            $query = $this->db->query("select @id as codProd");
            $max = $query->row();
            $codPro = $max->codProd;
            $sql1 = "call sp_I_PrecioProd(?,?,?,?,?)";
            foreach ($precios as $fila) {
                $params2 = array($codPro, $suc, $fila->cant, $fila->precio, $fila->codPre);
                $this->db->query($sql1, $params2);
            }
            if ($suc == 6) {
                $sql3 = 'call sp_cantFilas(?,?,?)';
                $params2 = array('almacen_producto', 'producto_id', $codPro);
                $query2 = $this->db->query($sql3, $params2);
                $row = $query2->row();
                if ($row->cant == 0) {
                    $sql4 = "call sp_I_CursorStockProd(?,?)";
                    foreach ($colors as $color) {
                        $params2 = array($codPro, $color->id);
                        $this->db->query($sql4, $params2);
                    }
                }
            }
            $this->db->trans_complete();
            $msg = $this->db->trans_status();
        } catch (Exception $exc) {
            $msg = false;
        }
        $resultado = array("valor" => $msg, "codigo" => $codPro);
        return $resultado;
    }

    public function U_StockProd($stk, $prod, $color) {
        $sql = "call sp_U_StockProd(?,?,?,?)";
        foreach ($stk as $value) {
            $params = array($value->cant, $value->codAlm, $prod, $color);
            $this->db->query($sql, $params);
        }
    }

    /*  public function IU_Producto($des, $modelo, $marca, $familia, $suc, $prod, $precios, $stock) {
      try {
      $params = array($des, $modelo, $marca, $familia, $suc);
      $this->db->trans_start();
      $sql = "call sp_IULogProducto(?,?,?,?,?,@id)";
      $this->db->query("select $prod into @id");
      $this->db->query($sql, $params);
      $query = $this->db->query("select @id as codProd");
      $max = $query->row();
      $codPro = $max->codProd;
      $sql1 = "call sp_I_PrecioProd(?,?,?,?,?)";
      foreach ($precios as $fila) {
      $params2 = array($codPro, $suc, $fila->cant, $fila->precio, $fila->codPre);
      $this->db->query($sql1, $params2);
      }
      $sql2 = "call sp_I_StockProd(?,?,?,?)";
      $cont = 0;
      foreach ($stock as $fila) {
      $params2 = array($fila->codAlm, $codPro, $fila->cant, $suc);
      $this->db->query($sql2, $params2);
      $cont++;
      }
      if ($suc == 6) {
      $sql3 = 'call sp_cantFilas(?,?,?)';
      $params2 = array('almacen_producto', 'producto_id', $codPro);
      $query2 = $this->db->query($sql3, $params2);
      $row = $query2->row();
      if (($row->cant) == $cont) {
      $sql4 = "call sp_I_CursorStockProd(?,?)";
      $params3 = array($codPro, $suc);
      $this->db->query($sql4, $params3);
      }
      }
      $this->db->trans_complete();
      $msg = $this->db->trans_status();
      } catch (Exception $exc) {
      $msg = false;
      }

      return $msg;
      } */
}

?>
